﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using DX_DailyUtil_1._0._1.Web.App_Ctrl;
using DX_DailyUtil_1._0._1.Web.App_Ctrl.objt.hitch;
using System.Collections.Generic;
using System.Data.Linq.Mapping;
using DX_DailyUtil_1._0._1.Web.App_Ctrl.logic;
using System.Data.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization.Json;
using DX_DailyUtil_1._0._1.Web.App_Ctrl.util;

namespace DX_DailyUtil_1._0._1.Web.WCF_Ctrl
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class WCF_Hitch
    {
        private KDB_dx_1_localDataContext mDB = new KDB_dx_1_localDataContext();

        [OperationContract]
        public void DoWork()
        {
            // 在此处添加操作实现
            return;
        }

        /// <summary>
        /// Hitch_record
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        public String get_All_hitch_record_By_none_In_Json()
        {
            var _result = from o in mDB.hitch_record select o;

            String _resultJson = DB_Util_Json.getJson_JS(_result);

            return _resultJson;
        }
        

        /// <summary>
        /// Hitch_record
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        public List<hitch_record> get_All_hitch_record_By_none_In_Entity()
        {
            var _result = from o in mDB.hitch_record
                          //from c in o.RL_state orderby c.name
                          select o;
            List<hitch_record> d = _result.ToList();
            return _result.ToList();
        }


        [OperationContract]
        public List<machine> get_All_machine_By_none()
        {
            var _result = from o in mDB.machine select o;

            return _result.ToList();
        }

        [OperationContract]
        public List<hitch_cause> get_All_hitch_cause_By_none_In_Entity()
        {
            var _result = from o in mDB.hitch_cause select o;

            return _result.ToList();
        }

        [OperationContract]
        public List<user> get_All_user_By_department_In_Entity(long aDepartmentId)
        {
            var _result = from o in mDB.user where o.section.FK_To_department == aDepartmentId select o;

            return _result.ToList();
        }

        [OperationContract]
        public List<client> get_All_client_By_department_In_Entity(long aDepartmentId)
        {
            var _result = from o in mDB.client where o.user.section.FK_To_department == aDepartmentId select o;

            return _result.ToList();
        }

        [OperationContract]
        public List<machine> get_All_machine_By_department_In_Entity(long aDepartmentId)
        {
            var _result = from o in mDB.machine where o.user.section.FK_To_department == aDepartmentId select o;

            return _result.ToList();
        }


        [OperationContract]
        public Binary upt_One_hitch_record_By_None(hitch_record aData)
        {
            //mDB.ObjectTrackingEnabled = false;
            //hitch_record _data = (from o in mDB.hitch_record where o.id == aData.id select o).First();
            //_data = aData;
            //mDB.ObjectTrackingEnabled = true;
            //var _result = (from o in mDB.hitch_record where o.id == aData.id select o).Single();
            mDB.hitch_record.Attach(aData, true);
            //mDB.hitch_record.Attach(_result, aData);
            //mDB.Refresh(RefreshMode.KeepCurrentValues, _data);
            mDB.SubmitChanges();
            return aData.time_check;
        }

        [OperationContract]
        public String ins_One_hitch_record_By_None(hitch_record aData)
        {
            mDB.hitch_record.InsertOnSubmit(aData);
            mDB.SubmitChanges();
            return "成功";
        }

        /////////////////////////////////////////////////////

        //[OperationContract]
        //public IQueryable<ISerializable> get_All_By_none(String aTableName)
        //{
        //    Type _t = Type.GetType(aTableName);
        //    var _result = from ISerializable o in mDB.GetTable(_t) select o;
        //    return _result;
        //}

        //[OperationContract]
        //public IQueryable<T> get_All_By_where_single<T>(String aTableName, String aField, String aValue)
        //{
        //    Type _t = Type.GetType(aTableName);
        //    var _result = from T o in mDB.GetTable(_t) where o.GetType().GetField(aField).GetValue(o).Equals(aValue) select o;
        //    return _result;
        //}

        //[OperationContract]
        //public IQueryable<T> get_All_By_where_array<T>(String aTableName, DB_Where aCondition)
        //{
        //    Type _t = Type.GetType(aTableName);
        //    var _result = from T o in mDB.GetTable(_t) where (aCondition.check<T>(o)) select o;
        //    return _result;
        //}

        [OperationContract]
        public String get_All_By_Condition_In_DataSet_In_Json(DB_Logic_Query aCondition)
        {
            Type _tableT = Type.GetType("DX_DailyUtil_1._0._1.Web.App_Ctrl." + aCondition.MTableName);
            ITable _table = mDB.GetTable(_tableT);
            MethodCallExpression _call = DB_Util_Expr.createExpr(_table, _tableT, aCondition);
            MethodCallExpression _select = DB_Util_Expr.createExpr_Select(_call, _table, _tableT, aCondition);

            var _result = mDB.GetTable(_tableT).Provider.CreateQuery(_select);
            //mDB.hitch_record.Where(o => o.id == 1).Select(o => new Object { });
            String _resultJson = DB_Util_Json.getJson_JS(_result);
            return _resultJson;

        }

        // 在此处添加更多操作并使用 [OperationContract] 标记它们
    }
}
